package org.walkerljl.config.impl.managed;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.walkerljl.config.abstracts.AbstractConfigurator;
import org.walkerljl.log.Logger;
import org.walkerljl.log.LoggerFactory;

/**
 *
 * 基于JVM可管理的配置器
 *
 * @author lijunlin
 */
public class JvmManagedConfigurator extends AbstractConfigurator {
	
	protected final Logger LOGGER = LoggerFactory.getLogger(getClass());
	
	/** 配置信息容器*/
	private Map<String, Object> propertiesMap = null;
	
	/**
	 * 构造函数
	 */
	public JvmManagedConfigurator() {
		propertiesMap = new ConcurrentHashMap<String, Object>();
	}

	@Override
	public Object getProperty(String key) {
		if (!validateKey(key)) {
			return null;
		}
		return propertiesMap.get(key);
	}
	
	@Override
	public void setProperty(String key, Object value) {
		if (validateKey(key)) {
			propertiesMap.put(wrapKey(key), value);
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(String.format("%s覆盖配置, Key:%s, Value:%s", getClass(), key, value));
			}
		} else {
			LOGGER.warn(String.format("%s无效的配置Key:%s", getClass(), key));
		}
	}
	
	@Override
	public void remove(String key) {
		if (validateKey(key)) {
			propertiesMap.remove(wrapKey(key));
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(String.format("%s移除配置, Key:%s", getClass(), key));
			}
		} else {
			LOGGER.warn(String.format("%s无效的配置Key:%s", getClass(), key));
		}
	}

	@Override
	public void clear() {
		propertiesMap = new ConcurrentHashMap<String, Object>();
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug(String.format("%s清除缓存", getClass()));
		}
	}
	
	/**
	 * 校验Key是否合法
	 * @param key
	 * @return
	 */
	private boolean validateKey(String key) {
		return key != null && !"".equals(key);
	}
}